using System;
using System.Linq;

namespace Glue.Internals.Verification
{
    internal class EnumRandomizer
    {
        private readonly Random random;

        public EnumRandomizer(Random random)
        {
            this.random = random;
        }

        public object Randomize(Type type)
        {
            if (!type.IsEnum)
                throw new GlueException("'"+type.Name+"' is not an Enum.");
            
            //Changed when creating Gluelight:
            //old var enumValues = Enum.GetValues(type);
            var enumValues = from field in type.GetFields()
                             where field.IsLiteral
                             select field;
 
            var list = enumValues.ToList();
            return list[random.Next(0,list.Count)].GetValue(type);
        }
    }
}